package com.example.xianji.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.JsonObjectRequest;
import com.example.xianji.Contant;
import com.example.xianji.XJApplication;
import com.example.xianji.R;
import com.example.xianji.bean.YiJi_Class;
import com.example.xianji.constant.Constant;
import com.example.xianji.single.Single;
import com.example.xianji.utils.sp.PreferenceEngine;
import com.example.xianji.widgets.CustomProgressDialog;

import android.os.Bundle;
import android.os.Build.VERSION;
import android.os.Build.VERSION_CODES;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Main_QiYe_informationActivity extends Activity {
    //    private SharedPreferences sp;
//    private Editor ed;
    private RelativeLayout qiye_jiben_xinxis_back;
    private TextView qiye_textview_nickname, qiye_textview_hangye, qiye_textview_guimo, qiye_textview_money, qiye_textview_man, qiye_textview_email, qiye_textview_mobile, qiye_textview_place, qiye_textview_jianjie, qiye_bianji, textview_guding_phone;
    private String qiye_province, qiye_city, qiye_distrct, tel, code, ext;
    private int qiye_province_id, qiye_city_id, qiye_distrct_id;
    private int qiye_guimo_id;
    private CustomProgressDialog mCustomProgressDialog;
    private LinearLayout linearlayout_hangyefenlei;
    private int indext = 1;


    private ArrayList<YiJi_Class> hangye_list = new ArrayList<YiJi_Class>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (VERSION.SDK_INT >= VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        setContentView(R.layout.activity_main__qi_ye_information);
//        sp = getSharedPreferences("text", MODE_PRIVATE);
//        ed = sp.edit();
        qiye_textview_nickname = (TextView) findViewById(R.id.qiye_textview_nickname);
        qiye_textview_hangye = (TextView) findViewById(R.id.qiye_textview_hangye);
        qiye_textview_guimo = (TextView) findViewById(R.id.qiye_textview_guimo);
        qiye_textview_money = (TextView) findViewById(R.id.qiye_textview_money);
        qiye_textview_man = (TextView) findViewById(R.id.qiye_textview_man);
        qiye_textview_email = (TextView) findViewById(R.id.qiye_textview_email);
        qiye_textview_mobile = (TextView) findViewById(R.id.qiye_textview_mobile);
        qiye_textview_place = (TextView) findViewById(R.id.qiye_textview_place);
        qiye_textview_jianjie = (TextView) findViewById(R.id.qiye_textview_jianjie);
        qiye_jiben_xinxis_back = (RelativeLayout) findViewById(R.id.qiye_jiben_xinxi_back);
        qiye_bianji = (TextView) findViewById(R.id.qiye_bianji);
        linearlayout_hangyefenlei = (LinearLayout) findViewById(R.id.linearlayout_hangyefenlei);
        textview_guding_phone = (TextView) findViewById(R.id.textview_guding_phone);

        mCustomProgressDialog = CustomProgressDialog.createDialog(this);
        mCustomProgressDialog.setCanceledOnTouchOutside(false);
        mCustomProgressDialog.show();
        get_qiye_information();

        qiye_jiben_xinxis_back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Main_QiYe_informationActivity.this.finish();
            }
        });
        qiye_bianji.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Main_QiYe_informationActivity.this, Main_Bianji_QiYe_InformationActivity.class);
                intent.putExtra("qiye_name", qiye_textview_nickname.getText().toString());
                intent.putExtra("qiye_hangye", qiye_textview_hangye.getText().toString());
                intent.putExtra("qiye_guimo", qiye_textview_guimo.getText().toString());
                intent.putExtra("qiye_guimo_id", qiye_guimo_id);
                intent.putExtra("qiye_money", qiye_textview_money.getText().toString());
                intent.putExtra("qiye_province", qiye_province);
                intent.putExtra("qiye_city", qiye_city);
                intent.putExtra("qiye_distrct", qiye_distrct);
                intent.putExtra("qiye_province_id", qiye_province_id);
                intent.putExtra("qiye_city_id", qiye_city_id);
                intent.putExtra("qiye_distrct_id", qiye_distrct_id);
                intent.putExtra("qiye_man_name", qiye_textview_man.getText().toString());
                intent.putExtra("qiye_email", qiye_textview_email.getText().toString());
                intent.putExtra("qiye_mobile", qiye_textview_mobile.getText().toString());
                intent.putExtra("tel", tel);
                intent.putExtra("code", code);
                intent.putExtra("ext", ext);
                intent.putExtra("qiye_jianjie", qiye_textview_jianjie.getText().toString());
                intent.putExtra("isMobile", Contant.has_mobile);
                startActivity(intent);
                Single.getInstance().setList(hangye_list);

            }
        });


        linearlayout_hangyefenlei.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
            }
        });
    }

    public void get_qiye_information() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("token", PreferenceEngine.getInstance().getString(Constant.SP.NAME, Constant.SP.KEY_XIANJI_TOKEN, ""));
        JSONObject parmas = new JSONObject(map);

        JsonObjectRequest request = new JsonObjectRequest(Method.POST, Contant.yonghu_infor,
                parmas, new Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {
                try {
                    if (response.getInt("status") == 1) {
                        JSONArray array = response.getJSONArray("data");
                        JSONObject object0 = array.getJSONObject(0);
                        JSONObject object1 = array.getJSONObject(1);
                        JSONObject object2 = array.getJSONObject(2);
                        qiye_textview_nickname.setText(object0.getString("nickname"));
                        qiye_textview_hangye.setText(object2.getJSONObject("hytags").getString("tag_name"));
                        if (object1.getInt("scope") == 1) {
                            qiye_textview_guimo.setText(">100人");
                        } else if (object1.getInt("scope") == 2) {
                            qiye_textview_guimo.setText("51-100人");
                        } else if (object1.getInt("scope") == 3) {
                            qiye_textview_guimo.setText("21-50人");
                        } else if (object1.getInt("scope") == 4) {
                            qiye_textview_guimo.setText("5-20人");
                        } else if (object1.getInt("scope") == 5) {
                            qiye_textview_guimo.setText("<5人");
                        }
                        qiye_guimo_id = object1.getInt("scope");

                        qiye_textview_money.setText(object1.getString("capital"));
                        qiye_textview_man.setText(object1.getString("name"));
                        qiye_textview_email.setText(object0.getString("email"));
                        qiye_textview_mobile.setText(object0.getString("mobile"));
                        qiye_textview_place.setText(object1.getString("province_name") + object1.getString("city_name") + object1.getString("district_name"));
                        qiye_province = object1.getString("province_name");
                        if (qiye_province.equals("上海") || qiye_province.equals("北京") || qiye_province.equals("天津")
                                || qiye_province.equals("香港") || qiye_province.equals("台湾")) {
                            qiye_city = qiye_province;
                            qiye_distrct = object1.getString("city_name");
                        } else {
                            qiye_city = object1.getString("city_name");
                            qiye_distrct = object1.getString("district_name");
                        }
                        qiye_textview_jianjie.setText(Html.fromHtml(object1.getString("info")));
                        qiye_province_id = object1.getInt("province");
                        if (qiye_province.equals("上海") || qiye_province.equals("北京") || qiye_province.equals("天津")
                                || qiye_province.equals("香港") || qiye_province.equals("台湾")) {
                            qiye_city_id = object1.getInt("city");
                            qiye_distrct_id = object1.getInt("district");

                        } else {
                            qiye_city_id = object1.getInt("city");
                            qiye_distrct_id = object1.getInt("district");
                        }

                        tel = object1.getString("tel");
                        code = object1.getString("code");
                        ext = object1.getString("ext");
                        if (tel.length() > 0) {
                            textview_guding_phone.setText(code + "--" + tel + "--" + ext);
                        } else {
                            textview_guding_phone.setText("请输入固定号码");
                        }
                    } else {
                    }
                    if (mCustomProgressDialog != null) {
                        mCustomProgressDialog.dismiss();
                        mCustomProgressDialog = null;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                if (mCustomProgressDialog != null) {
                    mCustomProgressDialog.dismiss();
                    mCustomProgressDialog = null;
                }
            }
        });
        XJApplication.getHttpQueues().cancelAll("abcPost");
        request.setTag("abcPost");
        request.setRetryPolicy(new DefaultRetryPolicy(50 * 1000, 1, 1.0f));
        XJApplication.getHttpQueues().add(request);
        XJApplication.getHttpQueues().start();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (Main_Bianji_QiYe_InformationActivity.save == 1) {
            get_qiye_information();
        }
        Main_Bianji_QiYe_InformationActivity.save = 0;
    }
}
